package cn.stylefeng.guns.modular.rest;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.hutool.core.codec.Base64;
import cn.stylefeng.guns.sys.core.constant.DefaultAvatar;
import cn.stylefeng.guns.sys.modular.system.entity.FileInfo;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum;

/**
 * 文件管理
 */
@Controller
@RequestMapping("/system")
public class WsystemController extends BaseController {

    @Autowired
    private FileInfoService fileInfoService;

    /**
     * 上传图片
     * @param file
     * @return
     */
    @RequestMapping("/uploads")
    @ResponseBody
    public ResponseData uploads(@RequestParam("file") MultipartFile file) {
        String fileId = this.fileInfoService.uploadFile(file);
        
        HashMap<String, Object> map = new HashMap<>();
        map.put("fileId", fileId);
        return ResponseData.success(0, "上传成功", map);
    }
    
    /**
     * 获取音频流
     *送达
     */
	@RequestMapping("audio_songda")
     public void audio_songda(HttpServletResponse response) {
         String path = "assets/audio/songda.mp3";
         try {
             InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
             IOUtils.copy(inputStream, response.getOutputStream());
             response.addHeader("Content-Type", "audio/mpeg;charset=utf-8");
             response.flushBuffer();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
    
    /**
     * 获取音频流
     * 离场
     */
	@RequestMapping("audio_lichang")
     public void audio_lichang(HttpServletResponse response) {
         String path = "assets/audio/lichang.mp3";
         try {
             InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
             IOUtils.copy(inputStream, response.getOutputStream());
             response.addHeader("Content-Type", "audio/mpeg;charset=utf-8");
             response.flushBuffer();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
    
    /**
     * 获取音频流
     * 到场
     */
	@RequestMapping("audio_daochang")
     public void audio_daochang(HttpServletResponse response) {
         String path = "assets/audio/daochang.mp3";
         try {
             InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
             IOUtils.copy(inputStream, response.getOutputStream());
             response.addHeader("Content-Type", "audio/mpeg;charset=utf-8");
             response.flushBuffer();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
    
    /**
     * 获取音频流
     * 成功
     */
	@RequestMapping("audio_success")
     public void audio_success(HttpServletResponse response) {
         String path = "assets/audio/success.mp3";
         try {
             InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
             IOUtils.copy(inputStream, response.getOutputStream());
             response.addHeader("Content-Type", "audio/mpeg;charset=utf-8");
             response.flushBuffer();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
    
    /**
     * 获取音频流
     * 失败
     */
	@RequestMapping("audio_error")
     public void audio_error(HttpServletResponse response) {
         String path = "assets/audio/error.mp3";
         try {
             InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
             IOUtils.copy(inputStream, response.getOutputStream());
             response.addHeader("Content-Type", "audio/mpeg;charset=utf-8");
             response.flushBuffer();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

    /**
     * 查看图片
     *
     * @author fengshuonan
     * @Date 2019/7/27 20:33
     */
    @RequestMapping("/preview/{fileId}")
    public Object preview(@PathVariable("fileId") String fileId, HttpServletResponse response) {

        byte[] decode = null;

        //输出图片的文件流
        try {
            response.setContentType("image/jpeg");
            decode = this.fileInfoService.getFileBytes(fileId);
        } catch (Exception e) {
            //返回默认图片
            decode = Base64.decode(DefaultAvatar.AVATAR);
        }

        try {
            response.getOutputStream().write(decode);
        } catch (IOException e) {
            throw new ServiceException(CoreExceptionEnum.SERVICE_ERROR);
        }

        return null;
    }

    /**
     * 预览头像
     */
    @RequestMapping("/previewa/{avatar}")
    public Object previewa(@PathVariable("avatar") String avatar, HttpServletResponse response) {
        byte[] decode = null;
        
        //输出图片的文件流
        response.setContentType("image/jpeg");
        decode = this.fileInfoService.getFileBytes(avatar);

        try {
            response.getOutputStream().write(decode);
        } catch (IOException e) {
            throw new ServiceException(CoreExceptionEnum.SERVICE_ERROR);
        }

        return null;
    }
    
    /**
     * 通过关联id 查询文件列表
     */
    @RequestMapping("/findFileListByrelation")
    @ResponseBody
    public ResponseData findFileListByrelation(Long relation) {
    	FileInfo fileInfo = new FileInfo();
    	fileInfo.setRelation(relation);
        return ResponseData.success(fileInfoService.findListBySpec(fileInfo));
    }
}
